// 配置路由的地方
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/pages/Home'
import Login from '@/pages/Login'
import Search from '@/pages/Search'
import Register from '@/pages/Register'
// 使用插件
Vue.use(VueRouter)

// 先把VueRouter原型对象的push，先保存一份
let originPush = VueRouter.prototype.push

// 重写push|replace
// 第一个参数：告诉原来push方法，你往哪里跳转（传递哪些参数）
VueRouter.prototype.push = function (location,resolve,reject){
    if(resolve && reject){
        // call|apply区别
        // 相同点，都可以调用函数一次，都可以篡改上下文一次
        // 不同点：call与apply传递参数用逗号隔开，apply方法执行，传递数组
        originPush.call(this,location,()=>{},()=>{})
    }else{
        originPush.call(this,location,()=>{},()=>{})
    }
}

const router = new VueRouter({
    routes:[
        {
            path: "/home",
            component: Home,
            meta:{show:true}
        },
        {
            path: "/login",
            component: Login,
            meta:{show:false}
        },
        {
            path: "/search/:keyword?",
            component: Search,
            meta:{show:true},
            name:"search",
        },
        {
            path: "/register",
            component: Register,
            meta:{show:false}
        },
        {
            path:"*",
            redirect: '/home'
        }
    ]
})

export default router